package com.taikang.hmp.hbusiness.diabetes.measure.pressure.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;

import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.hbusiness.diabetes.measure.pressure.bean.Pressure;
import com.taikang.hmp.hbusiness.diabetes.measure.pressure.service.IPressureService;
import com.taikang.hmp.hbusiness.diabetes.measure.pressure.service.PressureUtil;

/** 
 * 血压
 * author:qrp
 * time:2015年8月5日 上午11:05:03
 */
public class PressureServiceImpl extends BaseServiceImpl implements IPressureService{

	public String savePressure(Dto paramDto){
		Pressure pressure = new Pressure();
		pressure.setCustomer_id(paramDto.getAsString("customerid"));
		pressure.setDate(paramDto.getAsString("date"));
		String value = paramDto.getAsString("value");
		
		//分割收缩压和舒张压
		Integer low = null;
		Integer high = null;		
		String [] values = value.split("-");
		if(values.length!=2){
			throw new TKException("value must be format like 120-90.");
		}else{
			try{
				low = Integer.parseInt(values[1]);
				high = Integer.parseInt(values[0]);
			}catch(Exception e){
				throw new TKException("value must be format like 120-90.");
			}
		}
		
		pressure.setValue(value);
		String unit = paramDto.getAsString("unit");
		List<Pressure> info = super.getNewDao().queryForList("pressure_space.getPressure", pressure);
		if(info.size() > 0){
			pressure.setRecord_id(info.get(0).getRecord_id());
			pressure.setUnit(unit);
			pressure.setOperater(paramDto.getAsString("uid"));
			pressure.setOperate_date(new Date());
			pressure.setSource_cd(paramDto.getAsString("requestsource"));
			super.getNewDao().update("pressure_space.update_pressure", pressure);
		}else{
			String record_id = java.util.UUID.randomUUID().toString();
			pressure.setRecord_id(record_id);
			pressure.setUnit(unit);
			pressure.setSource_cd(paramDto.getAsString("requestsource"));
			pressure.setOperater(paramDto.getAsString("uid"));
			pressure.setOperate_date(new Date());
			super.getNewDao().insert("pressure_space.insert_pressure", pressure);
		}
		//返回评判结果
		String pressureWords = PressureUtil.getPressureWords(high,low);
		return pressureWords;
	}

	public List<Dto> getPressureList(Dto paramDto) {
		Map<String, String> condition = new HashMap<String, String>();
		condition.put("customerid", paramDto.getAsString("customerid"));
		if(paramDto.getAsString("startdate")==null || paramDto.getAsString("startdate").equals("")){
			condition.put("start_date", "1900-01-01 00:00");
		}else{
			condition.put("start_date", paramDto.getAsString("startdate") + " 00:00");
		}
		condition.put("end_date", paramDto.getAsString("enddate") + " 23:59");
		List<Pressure> info = super.getNewDao().queryForList("pressure_space.getPressureList", condition);
		List<Dto> result = new ArrayList<Dto>();
		for (int i = 0; i < info.size(); i++) {
			Dto resultMap = new BaseDto();
			resultMap.put("date", info.get(i).getDate());
			resultMap.put("value", info.get(i).getValue());
			resultMap.put("unit", info.get(i).getUnit());
			result.add(resultMap);
		}
		return result;
	}

	public List<Pressure> getPressureChartList(Dto dto) {
		Pressure pressure = new Pressure();

		pressure.setCustomer_id(dto.getAsString("customerid"));
		if ("1".equals(dto.getAsString("day"))) {
			pressure.setDates(dto.getAsString("date"));
		} else {
			pressure.setBef_date(dto.getAsString("befdate") + " 00:00");
			pressure.setAft_date(dto.getAsString("aftdate") + " 23:59");
		}
		return super.getNewDao().queryForList("pressure_space.getPressureChartList", pressure);
	}

	public String delPressure(Dto paramDto) {
		String temp = paramDto.getAsString("record_id");
		String[] record_ids = temp.split(",");
		for(int i = 0 ;i<record_ids.length;i++){
			super.getNewDao().delete("pressure_space.delPressure", record_ids[i]);
		}
		return "success";
	}

}
